﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing

Public Class ClsMMLinkNode
    Inherits DiagramLink
    'represents my own diagram link

#Region "Attributes"
#Region "Private attributes"
    Private MeRelationType As kkRelationTypes = kkRelationTypes.kkNone 'type of relation
    Private MeTypeOfLink As GeTypeOfLink = GeTypeOfLink.kkRelationship 'type of link
#End Region
#End Region
#Region "Constructors"
    ' ''Protected Sub New()
    ' ''    MyBase.New()
    ' ''End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram)
        MyBase.new(parent)
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram, ByVal origin As MindFusion.Diagramming.DiagramNode, ByVal destination As MindFusion.Diagramming.DiagramNode)
        MyBase.new(parent, origin, destination)
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram, ByVal origin As MindFusion.Diagramming.DiagramNode, ByVal destination As System.Drawing.PointF)
        MyBase.New(parent, origin, destination)
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram, ByVal src As System.Drawing.PointF, ByVal dest As MindFusion.Diagramming.DiagramNode)
        MyBase.New(parent, src, dest)
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram, ByVal src As System.Drawing.PointF, ByVal dest As System.Drawing.PointF)
        MyBase.New(parent, src, dest)
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal prototype As MindFusion.Diagramming.DiagramLink, ByVal src As MindFusion.Diagramming.DiagramNode, ByVal dest As MindFusion.Diagramming.DiagramNode)
        MyBase.New(prototype, src, dest)
    End Sub
#End Region
#Region "Properties"
#Region "Public properties"
    Public Property MMRelationType() As kkRelationTypes
        Get
            Return MeRelationType
        End Get
        Set(ByVal IeValue As kkRelationTypes)
            MeRelationType = IeValue
        End Set
    End Property
    Public Property MMTypeOfLink() As GeTypeOfLink
        Get
            Return MeTypeOfLink
        End Get
        Set(ByVal IeValue As GeTypeOfLink)
            MeTypeOfLink = IeValue
            Me.Pen = GetPenForLink()
        End Set
    End Property
#End Region
#End Region
#Region "Subs"
#Region "Overrides & Overloads Subs"
    'overrided sub for saving to XML
    Protected Overrides Sub SaveToXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.SaveToXml(xmlElement, context)
        context.WriteEnum(MMTypeOfLink, "TypeOfLink", xmlElement)
        context.WriteEnum(MMRelationType, "RelationType", xmlElement)
    End Sub
    'overrided sub for loading from XML
    Protected Overrides Sub LoadFromXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.LoadFromXml(xmlElement, context)
        'assigning values directly to variables not to property to prevent changing attributes after deserialization!
        MeTypeOfLink = context.ReadEnum("TypeOfLink", xmlElement)
        MeRelationType = context.ReadEnum("RelationType", xmlElement)
    End Sub
    'overrided sub for drawing a link
    Public Overrides Sub Draw(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal options As MindFusion.Diagramming.RenderOptions)
        MyBase.Draw(graphics, options)
        MMDrawLinkRelationship(graphics)
    End Sub
    'overrided sub called after deserialization of link. I can set some properties forcible here!
    Protected Overrides Sub OnLoad()
        MyBase.OnLoad()
    End Sub
#End Region
#Region "MM Subs"
    'default settings of link
    Public Sub MMSetDefaultProperties()
        MMRelationType = kkRelationTypes.kkNone
        MMTypeOfLink = GeTypeOfLink.kkRelationship 'pen is set here
        'these 5 settings are valid only before link is created. After creation settings are set to different values
        Me.SnapToNodeBorder = False 'whether the link end points should automatically align to node borders while a user moves them
        Me.AutoRoute = False 'whether a link should avoid nodes by going the shortest path from its origin to its destination without crossing any other nodes
        Me.Style = LinkStyle.Polyline  'style for the segments of links
        'these 5 settings are valid only before link is created. After creation settings are set to different values

        Me.AllowMoveStart = True 'whether the user is allowed to move the first control point of a link
        Me.AllowMoveEnd = True 'whether the user is allowed to move the end point of a link
        Me.Dynamic = True 'whether a link automatically adjusts its end points positions in order to keep pointing to the centers of nodes that it connects
        Me.ShadowOffsetX = 0.0 'horizontal offset of the shadow
        Me.ShadowOffsetY = 0.0 'vertical offset of the shadow
        Me.CustomDraw = CustomDraw.Additional 'the custom painting code executes after item's interior and frame are painted by FlowChart.NET, but before text is rendered. In this mode, the Graphics instance passed to the custom drawing event handler has a clipping region applied to it
        Me.HeadShape = ArrowHead.None 'no arrow head
        Me.Text = GsStringDefaultValue   'label of link
        Me.TextStyle = LinkTextStyle.OverLongestSegment 'position and alignment of links labels
        Me.ToolTip = Me.Text 'tooltip of link
    End Sub
    'drawing relationship N
    Private Sub MMDrawLinkRelationshipN(ByRef IoGraphics As MindFusion.Drawing.IGraphics, ByVal InPointX As Single, ByVal InPointY As Single, ByVal IoBounds As RectangleF)
        Dim LoPen As Drawing.Pen = New Drawing.Pen(Me.Pen.Color, Me.Pen.Width)
        'on what edge to draw relationship
        Dim LeEdge As GeEdgeForLink = GetEdgeForLink(InPointX, InPointY, IoBounds)

        Select Case LeEdge
            Case GeEdgeForLink.kkNoEdge
                'nowhere
                'do nothing
            Case GeEdgeForLink.kkLeft
                'left
                IoGraphics.DrawLine(LoPen, InPointX - 4, InPointY, InPointX, InPointY - 2) 'top line
                IoGraphics.DrawLine(LoPen, InPointX - 4, InPointY, InPointX, InPointY + 2) 'bottom line
                IoGraphics.DrawLine(LoPen, InPointX - 4, InPointY, InPointX, InPointY) 'middle line
                IoGraphics.DrawLine(LoPen, InPointX - 4, InPointY - 2, InPointX - 4, InPointY + 2) 'vertical line
            Case GeEdgeForLink.kkRight
                'right
                IoGraphics.DrawLine(LoPen, InPointX, InPointY - 2, InPointX + 4, InPointY) 'top line
                IoGraphics.DrawLine(LoPen, InPointX, InPointY + 2, InPointX + 4, InPointY) 'bottom line
                IoGraphics.DrawLine(LoPen, InPointX, InPointY, InPointX + 4, InPointY) 'middle line
                IoGraphics.DrawLine(LoPen, InPointX + 4, InPointY - 2, InPointX + 4, InPointY + 2) 'vertical line
            Case GeEdgeForLink.kkTop
                'top
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY, InPointX, InPointY - 4) 'left line
                IoGraphics.DrawLine(LoPen, InPointX + 2, InPointY, InPointX, InPointY - 4) 'right line
                IoGraphics.DrawLine(LoPen, InPointX, InPointY - 4, InPointX, InPointY) 'middle line
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY - 4, InPointX + 2, InPointY - 4) 'horizontal line
            Case GeEdgeForLink.kkBottom
                'bottom
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY, InPointX, InPointY + 4) 'left line
                IoGraphics.DrawLine(LoPen, InPointX + 2, InPointY, InPointX, InPointY + 4) 'right line
                IoGraphics.DrawLine(LoPen, InPointX, InPointY + 4, InPointX, InPointY) 'middle line
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY + 4, InPointX + 2, InPointY + 4) 'horizontal line
        End Select
    End Sub
    'drawing relationship 1
    Private Sub MMDrawLinkRelationship1(ByVal IoGraphics As MindFusion.Drawing.IGraphics, ByVal InPointX As Single, ByVal InPointY As Single, ByVal IoBounds As RectangleF)
        Dim LoPen As Drawing.Pen = New Drawing.Pen(Me.Pen.Color, Me.Pen.Width)
        'on what edge to draw relationship
        Dim LeEdge As GeEdgeForLink = GetEdgeForLink(InPointX, InPointY, IoBounds)

        Select Case LeEdge
            Case GeEdgeForLink.kkNoEdge
                'do nothing
            Case GeEdgeForLink.kkLeft
                'left
                IoGraphics.DrawLine(LoPen, InPointX - 4, InPointY - 2, InPointX - 4, InPointY + 2) 'vertical line
            Case GeEdgeForLink.kkRight
                'right
                IoGraphics.DrawLine(LoPen, InPointX + 4, InPointY - 2, InPointX + 4, InPointY + 2) 'vertical line
            Case GeEdgeForLink.kkTop
                'top
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY - 4, InPointX + 2, InPointY - 4) 'horizontal line
            Case GeEdgeForLink.kkBottom
                'bottom
                IoGraphics.DrawLine(LoPen, InPointX - 2, InPointY + 4, InPointX + 2, InPointY + 4) 'horizontal line
        End Select
    End Sub
    'drawing any relationship
    Private Sub MMDrawLinkRelationship(ByVal IoGraphics As MindFusion.Drawing.IGraphics)
        If Me.IsConnected Then 'draw both marks at link only if the link is connected to nodes at both its ends
            Select Case Me.MMRelationType
                Case kkRelationTypes.kkNone 'GeTypeOfLink.kkNone 
                    'do nothing
                Case kkRelationTypes.kk_1_01 'GeTypeOfLink.kkLink1a01
                    'If Me.Origin IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(0).X, Me.ControlPoints.GetAt(0).Y, Me.Origin.Bounds)
                    'End If
                    'If Me.Destination IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).X, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).Y, Me.Destination.Bounds)
                    'End If
                Case kkRelationTypes.kk_1_1 'GeTypeOfLink.kkLink1a1
                    'If Me.Origin IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(0).X, Me.ControlPoints.GetAt(0).Y, Me.Origin.Bounds)
                    'End If
                    'If Me.Destination IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).X, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).Y, Me.Destination.Bounds)
                    'End If
                Case kkRelationTypes.kk_1_1n 'GeTypeOfLink.kkLink1a1N
                    'If Me.Origin IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(0).X, Me.ControlPoints.GetAt(0).Y, Me.Origin.Bounds)
                    'End If
                    'If Me.Destination IsNot Nothing Then
                    MMDrawLinkRelationshipN(IoGraphics, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).X, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).Y, Me.Destination.Bounds)
                    'End If
                Case kkRelationTypes.kk_1_n 'GeTypeOfLink.kkLink1aN
                    'If Me.Origin IsNot Nothing Then
                    MMDrawLinkRelationship1(IoGraphics, Me.ControlPoints.GetAt(0).X, Me.ControlPoints.GetAt(0).Y, Me.Origin.Bounds)
                    'End If
                    'If Me.Destination IsNot Nothing Then
                    MMDrawLinkRelationshipN(IoGraphics, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).X, Me.ControlPoints.GetAt(Me.ControlPoints.Count - 1).Y, Me.Destination.Bounds)
                    'End If
                Case Else
                    Debug.Print("CHYBA!!! - drawLinkRelationship")
            End Select
        End If
    End Sub
#End Region
#End Region
#Region "Functions"
#Region "MM Functions"
    'changing name of link
    Public Function MMChangeName(ByVal IsName As String) As GeActionResult
        Me.Text = IsName
        Return GeActionResult.kkOk
    End Function
    '' ''gets pen of link for note
    ' ''Function GetPenOfLinkForNote() As MindFusion.Drawing.Pen
    ' ''    Dim LoPen As MindFusion.Drawing.Pen = MFPenVirginCreator(Color.Gray, 0.1)
    ' ''    LoPen.DashStyle = Drawing2D.DashStyle.Custom
    ' ''    Dim LoDashPattern() As Single = {5, 5}
    ' ''    LoPen.DashPattern = LoDashPattern

    ' ''    Return LoPen
    ' ''End Function
    'gets pen for link
    Private Function GetPenForLink()
        Select Case MeTypeOfLink
            Case GeTypeOfLink.kkRelationship
                Dim LoPen As MindFusion.Drawing.Pen = MFPenVirginCreator(Color.DimGray, 0.1)
                LoPen.DashStyle = Drawing2D.DashStyle.Solid
                Return LoPen
            Case GeTypeOfLink.kkNote
                Dim LoPen As MindFusion.Drawing.Pen = MFPenVirginCreator(Color.Coral, 0.1)
                LoPen.DashStyle = Drawing2D.DashStyle.Custom
                Dim LoDashPattern() As Single = {5, 5}
                LoPen.DashPattern = LoDashPattern
                Return LoPen
            Case Else
                Debug.Print("CHYBA!!! - GetLinkStyleForLink")
                Return Nothing
        End Select
    End Function
#End Region
#End Region

End Class
